import { combineSlices, configureStore } from '@reduxjs/toolkit';

import { appSlice } from './modules/appStore';
import { themeSlice } from './modules/themeStore';
import { userSlice } from './modules/userStore';

import type { Action, ThunkAction } from '@reduxjs/toolkit';

/** 合并所有模块的 slice，生成根 reducer combineSlices 会自动根据每个 slice 的 reducerPath 进行合并 无需手动调用 combineReducers */
const rootReducer = combineSlices(themeSlice, appSlice, userSlice);

/** 推断全局 RootState 类型 RootState 表示整个 Redux 状态树的类型 */
export type RootState = ReturnType<typeof rootReducer>;

/** 创建全局唯一的 Redux store 实例 */
const store = configureStore({
	reducer: rootReducer
});

/** 推断 store 类型 */
export type AppStore = typeof store;

/** 推断全局 dispatch 类型 */
export type AppDispatch = AppStore['dispatch'];

/**
 * 通用 Thunk 类型定义
 *
 * @template ReturnType 异步返回值类型，默认为 void
 */

export type AppThunk<ReturnType = void> = ThunkAction<ReturnType, RootState, unknown, Action>;

export default store;
